﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public partial class frmRequestForm : DevExpress.XtraEditors.XtraForm
    {
        List<Book> lBook = new List<Book>();
        private Account acc;
        private frmTrangChu frmTrangChu;
        public frmRequestForm()
        {
            InitializeComponent();
        }

        public frmRequestForm(Account acc, frmTrangChu frm)
        {
            InitializeComponent();
            this.acc = acc;
            this.frmTrangChu = frm;
        }

        public frmRequestForm(List<Book> lBookRequest, Account acc, frmTrangChu frm)
        {
            // TODO: Complete member initialization
            this.lBook = lBookRequest;
            InitializeComponent();
            this.acc = acc;
            this.frmTrangChu = frm;
        }

        private void frmRequestForm_Load(object sender, EventArgs e)
        {
            bookDataGridView.DataSource = lBook;
            SetNumber(bookDataGridView);
            Member reader = new Member();
            reader = DAO_Member.SelectMember(acc);
            nameTextBox.Text = reader.Name;
            emailTextBox.Text = reader.Email;
            phoneNumberTextBox.Text = reader.PhoneNumber;
        }

        private void SetNumber(DataGridView gridViewBook)
        {
            for (int i = 0; i < gridViewBook.RowCount; ++i)
                gridViewBook.Rows[i].Cells["colSTT"].Value = (i + 1).ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmTraCuuSach(lBook, acc, frmTrangChu);
            frm.Show();
            this.Hide();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            lBook.RemoveAt(bookDataGridView.CurrentRow.Index);
            bookDataGridView.DataSource = new List<Book>();
            bookDataGridView.DataSource = lBook;
            SetNumber(bookDataGridView);
        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            if (lBook.Count > 3 || lBook.Count == 0)
                MessageBox.Show("Số lượng sách của bạn không phù hợp...");
            else
            {
                RequestForm rq = new RequestForm();
                if (DAO_RequestForm.AddBookRequest(emailTextBox.Text, lBook))
                {
                    MessageBox.Show("Đã đăng ký mượn sách thành công...Nhấn vào nút home quay về trang chủ...");
                    btnDangKy.Enabled = false;
                }
            }
        }

        private void pictureEdit2_Click(object sender, EventArgs e)
        {
            var frm = new frmTrangChu(acc);
            frm.Show();
            this.Hide();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            //RequestForm rq = DAO_RequestForm.SelectRFByEmail(emailTextBox.Text);
            var config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(RequestForm)).CascadeOnUpdate(true);
            IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
            RequestForm template = new RequestForm();
            template.Reader.Email = emailTextBox.Text;
            IObjectSet result = db.QueryByExample(template);
            RequestForm rq = (RequestForm)result.Next();
            MessageBox.Show(rq.lBookRequest[0].bookRequest.TenSach);
            rq.lBookRequest[0].IsBorrow = true;
            db.Store(rq);
            db.Close();
            
            //lBookRequestBindingSource.DataSource = new List<BookRequest>();
            //RequestForm rqf = DAO_RequestForm.SelectRFByEmail(emailTextBox.Text);
            //lBookRequestBindingSource.DataSource = rqf.lBookRequest;
        }
    }
}